package com.jsh.action.basic;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.dao.DataAccessException;
import com.jsh.base.BaseAction;
import com.jsh.base.Log;
import com.jsh.model.po.Test;
import com.jsh.model.po.Logdetails;
import com.jsh.model.vo.basic.TestModel;
import com.jsh.service.basic.TestIService;
import com.jsh.service.basic.UserBusinessIService;
import com.jsh.util.PageUtil;

/*
 * 测试管理
 * @author jishenghua  qq:7-5-2-7-1-8-9-2-0
*/
@SuppressWarnings("serial")
public class TestAction extends BaseAction<TestModel> {
	private TestIService testService;
	private UserBusinessIService userBusinessService;
	private TestModel model = new TestModel();

	/**
	 * 增加测试
	 * 
	 * @return
	 */
	public void create() {
		Log.infoFileSync("==================开始调用增加测试信息方法create()===================");
		Boolean flag = false;
		try {
			Test test = new Test();
			test.setName(model.getName());
			testService.create(test);

			// ========标识位===========
			flag = true;
			// 记录操作日志使用
			tipMsg = "成功";
			tipType = 0;
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>>>>>>>>>增加测试信息异常", e);
			flag = false;
			tipMsg = "失败";
			tipType = 1;
		} finally {
			try {
				toClient(flag.toString());
			} catch (IOException e) {
				Log.errorFileSync(">>>>>>>>>>>>增加测试信息回写客户端结果异常", e);
			}
		}

		logService.create(
				new Logdetails(getUser(), "增加测试", model.getClientIp(), new Timestamp(System.currentTimeMillis()),
						tipType, "增加测试名称为  " + model.getName() + " " + tipMsg + "！", "增加测试" + tipMsg));
		Log.infoFileSync("==================结束调用增加测试方法create()===================");
	}

	/**
	 * 删除测试
	 * 
	 * @return
	 */
	public String delete() {
		Log.infoFileSync("====================开始调用删除测试信息方法delete()================");
		try {
			testService.delete(model.getRoleID());
			tipMsg = "成功";
			tipType = 0;
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>删除ID为 " + model.getRoleID() + "  的测试异常", e);
			tipMsg = "失败";
			tipType = 1;
		}
		model.getShowModel().setMsgTip(tipMsg);
		logService.create(
				new Logdetails(getUser(), "删除测试", model.getClientIp(), new Timestamp(System.currentTimeMillis()),
						tipType, "删除测试ID为  " + model.getRoleID() + " " + tipMsg + "！", "删除测试" + tipMsg));
		Log.infoFileSync("====================结束调用删除测试信息方法delete()================");
		return SUCCESS;
	}

	/**
	 * 更新测试
	 * 
	 * @return
	 */
	public void update() {
		Boolean flag = false;
		try {
			Test test = testService.get(model.getRoleID());
			test.setName(model.getName());
			testService.update(test);

			flag = true;
			tipMsg = "成功";
			tipType = 0;
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>>>修改测试ID为 ： " + model.getRoleID() + "信息失败", e);
			flag = false;
			tipMsg = "失败";
			tipType = 1;
		} finally {
			try {
				toClient(flag.toString());
			} catch (IOException e) {
				Log.errorFileSync(">>>>>>>>>>>>修改测试回写客户端结果异常", e);
			}
		}
		logService.create(
				new Logdetails(getUser(), "更新测试", model.getClientIp(), new Timestamp(System.currentTimeMillis()),
						tipType, "更新测试ID为  " + model.getRoleID() + " " + tipMsg + "！", "更新测试" + tipMsg));
	}

	/**
	 * 批量删除指定ID测试
	 * 
	 * @return
	 */
	public String batchDelete() {
		try {
			testService.batchDelete(model.getRoleIDs());
			model.getShowModel().setMsgTip("成功");
			// 记录操作日志使用
			tipMsg = "成功";
			tipType = 0;
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>批量删除测试ID为：" + model.getRoleID() + "信息异常", e);
			tipMsg = "失败";
			tipType = 1;
		}

		logService.create(
				new Logdetails(getUser(), "批量删除测试", model.getClientIp(), new Timestamp(System.currentTimeMillis()),
						tipType, "批量删除测试ID为  " + model.getRoleID() + " " + tipMsg + "！", "批量删除测试" + tipMsg));
		return SUCCESS;
	}

	/**
	 * 检查输入名称是否存在
	 */
	public void checkIsNameExist() {
		Boolean flag = false;
		try {
			flag = testService.checkIsNameExist("name", model.getName(), "Id", model.getRoleID());
		} catch (DataAccessException e) {
			Log.errorFileSync(
					">>>>>>>>>>>>>>>>>检查测试名称为：" + model.getName() + " ID为： " + model.getRoleID() + " 是否存在异常！");
		} finally {
			try {
				toClient(flag.toString());
			} catch (IOException e) {
				Log.errorFileSync(
						">>>>>>>>>>>>回写检查测试名称为：" + model.getName() + " ID为： " + model.getRoleID() + " 是否存在异常！", e);
			}
		}
	}

	/**
	 * 查找测试信息
	 * 
	 * @return
	 */
	public void findBy() {
		try {
			PageUtil<Test> pageUtil = new PageUtil<Test>();
			pageUtil.setPageSize(model.getPageSize());
			pageUtil.setCurPage(model.getPageNo());
			pageUtil.setAdvSearch(getCondition());
			testService.find(pageUtil);
			List<Test> dataList = pageUtil.getPageList();

			// 开始拼接json数据
			// {"total":28,"rows":[
			// {"productid":"AV-CB-01","attr1":"Adult Male","itemid":"EST-18"}
			// ]}
			JSONObject outer = new JSONObject();
			outer.put("total", pageUtil.getTotalCount());
			// 存放数据json数组
			JSONArray dataArray = new JSONArray();
			if (null != dataList) {
				for (Test test : dataList) {
					JSONObject item = new JSONObject();
					item.put("Id", test.getId());
					// 供应商名称
					item.put("Name", test.getName());
					item.put("op", 1);
					dataArray.add(item);
				}
			}
			outer.put("rows", dataArray);
			// 回写查询结果
			toClient(outer.toString());
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>>>>>>>>>查找测试信息异常", e);
		} catch (IOException e) {
			Log.errorFileSync(">>>>>>>>>>>>>>>>>>>回写查询测试信息结果异常", e);
		}
	}

	/**
	 * 用户对应测试显示
	 * 
	 * @return
	 */
	public void findUsertest() {
		try {
			PageUtil<Test> pageUtil = new PageUtil<Test>();
			pageUtil.setPageSize(100);
			// pageUtil.setCurPage(model.getPageNo());

			pageUtil.setAdvSearch(getCondition_Usertest());
			testService.find(pageUtil);
			List<Test> dataList = pageUtil.getPageList();

			// 开始拼接json数据
			JSONObject outer = new JSONObject();
			outer.put("id", 1);
			outer.put("text", "测试列表");
			outer.put("state", "open");
			// 存放数据json数组
			JSONArray dataArray = new JSONArray();
			if (null != dataList) {
				for (Test test : dataList) {
					JSONObject item = new JSONObject();
					item.put("id", test.getId());
					item.put("text", test.getName());
					// 勾选判断1
					Boolean flag = false;
					try {
						flag = userBusinessService.checkIsUserBusinessExist("Type", model.getUBType(), "KeyId",
								model.getUBKeyId(), "Value", "[" + test.getId().toString() + "]");
					} catch (DataAccessException e) {
						Log.errorFileSync(">>>>>>>>>>>>>>>>>设置用户对应的测试：类型" + model.getUBType() + " KeyId为： "
								+ model.getUBKeyId() + " 存在异常！");
					}
					if (flag == true) {
						item.put("checked", true);
					}
					// 结束
					dataArray.add(item);
				}
			}
			outer.put("children", dataArray);
			// 回写查询结果
			toClient("[" + outer.toString() + "]");
		} catch (DataAccessException e) {
			Log.errorFileSync(">>>>>>>>>>>>>>>>>>>查找测试异常", e);
		} catch (IOException e) {
			Log.errorFileSync(">>>>>>>>>>>>>>>>>>>回写查询测试结果异常", e);
		}
	}

	/**
	 * 拼接搜索条件
	 * 
	 * @return
	 */
	private Map<String, Object> getCondition() {
		/**
		 * 拼接搜索条件
		 */
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("Name_s_like", model.getName());
		condition.put("Id_s_order", "asc");
		return condition;
	}

	/**
	 * 拼接搜索条件-用户对应测试
	 * 
	 * @return
	 */
	private Map<String, Object> getCondition_Usertest() {
		/**
		 * 拼接搜索条件
		 */
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("Id_s_order", "asc");
		return condition;
	}

	// =============以下spring注入以及Model驱动公共方法，与Action处理无关==================
	@Override
	public TestModel getModel() {
		return model;
	}

	

	public void setTestService(TestIService testService) {
		this.testService = testService;
	}

	public void setUserBusinessService(UserBusinessIService userBusinessService) {
		this.userBusinessService = userBusinessService;
	}
}
